﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Lant.Net.Model;

namespace Lant.Net.Game
{
	public partial class Ant : Puke
	{
		private readonly int CARDCOUNT = 5;
		private const string _GameName = "蚂蚁上树 v1.0";
		private const string _GameRule = "只要比她的牌的牌面值大一就行";
		
		private Puker puker;
		private PukeInfo pukeInfo;
		private List<PukeCard> my = null, he = null, publicCard = null, myRecord = null, heRecord = null;

		public PukeInfo PukeInfo { get { return pukeInfo; } }
		public Ant()
		{
			InitializeComponent();
			pukeInfo = new PukeInfo30x40();
			puker = new Puker(pukeInfo, PukerMode.puke52);
			btnSend = new Button();
			btnSend.Top = 150;
			btnSend.Left = 135;
			btnSend.Width = 40;
			btnSend.Text = "出牌";
			btnSend.Click += new EventHandler(btnSend_Click);
			btnGet = new Button();
			btnGet.Top = 150;
			btnGet.Left = 180;
			btnGet.Width = 40;
			btnGet.Text = "过";
			btnGet.Click += new EventHandler(btnGet_Click);
		}

		#region 覆写属性和方法
		public override string GameName { get { return _GameName; } }
		public override string GameRule { get { return _GameRule; } }
		public override bool Start()
		{
			puker.Start();
			if (my != null) my.Clear();
			my = puker.GetPukeCard(this.CARDCOUNT);
			he = puker.GetPukeCard(this.CARDCOUNT);
			publicCard = new List<PukeCard>();
			myRecord = new List<PukeCard>();
			heRecord = new List<PukeCard>();

			this.UpdateForm();
			isFirst = true;
			return true;
		}
		#endregion

		private int heNumber = -1;
		private int NumberCount = 1;
		private bool isFirst = true;
		private bool isHeOver = false;
		private string state = "";
		Button btnSend;
		Button btnGet;
		void btnSend_Click(object sender, EventArgs e)
		{
			if (!this.btnSend.Enabled) return;
			bool heisStart = false;
			List<PukeCard> temp = this.GetSelected(my);
			if (temp.Count < 1)
			{
				//MessageBox.Show("请选择出牌");
				return;
			}
			//MessageBox.Show(temp[0].NumberDisplay);
			if (isFirst || isHeOver || (temp[0].PrevNumber == heNumber && temp.Count == NumberCount))
			{
				int tempCount = 0;
				PukeCard[] ps = new PukeCard[my.Count];
				my.CopyTo(ps);
				for (int i = 0; i < ps.Length; i++)
				{
					if (ps[i].Selected)
					{
						tempCount += 1;
						//if (tempCount > NumberCount && !isFirst) break;
						if (tempCount > NumberCount) break;
						publicCard.Add(ps[i]);
						my.Remove(ps[i]);
					}
				}
				//if (isFirst) NumberCount = tempCount;
				heisStart = this.heStart(temp[0]);
			}
			else
			{
				MessageBox.Show(string.Format("你出的牌不对，她出了[{0}]，你应该比他大一张的牌", pukeInfo.GetCardNumberDisplay(heNumber)), "提示");
			}
			this.UpdateForm();

			if (my.Count == 0 && !heisStart)
				this.gameOver("my");
		}

		void btnGet_Click(object sender, EventArgs e)
		{
			//int x = 0;
			List<PukeCard> temp = new List<PukeCard>();
			for (int i = 0; i < publicCard.Count; i++)
			{
				int tempNumber = publicCard[i].Number;
				if (tempNumber == 5 || tempNumber == 10 || tempNumber == 13)
				{
					temp.Add(publicCard[i]);
				}
			}

			//MessageBox.Show(x.ToString());
			//MessageBox.Show(temp.Count.ToString());
			publicCard.Clear();

			if (state == "get")
			{//他没牌了
				btnSend.Enabled = true;
				state = "";
				btnGet.Text = "过";
				heNumber = -1;
				myRecord.AddRange(temp);
				my.AddRange(puker.GetPukeCard(this.CARDCOUNT - my.Count));
				he.AddRange(puker.GetPukeCard(this.CARDCOUNT - he.Count));
			}
			else
			{//我点pass
				heRecord.AddRange(temp);
				he.AddRange(puker.GetPukeCard(this.CARDCOUNT - he.Count));
				my.AddRange(puker.GetPukeCard(this.CARDCOUNT - my.Count));
				this.heStart(null);
			}
			
			
			this.UpdateForm();
		}

		#region 私有方法
		private void gameOver()
		{
			this.gameOver("");
		}
		private void gameOver(string m)
		{
			int heCount = 0, myCount = 0;
			for (int i = 0; i < heRecord.Count; i++)
			{
				if (heRecord[i].Number == 5)
				{
					heCount += 5;
				}
				else if (heRecord[i].Number == 10 || heRecord[i].Number == 13)
				{
					heCount += 10;
				}
			}
			for (int i = 0; i < myRecord.Count; i++)
			{
				if (myRecord[i].Number == 5)
				{
					myCount += 5;
				}
				else if (myRecord[i].Number == 10 || myRecord[i].Number == 13)
				{
					myCount += 10;
				}
			}
			string mResult = "";
			if (myCount > heCount)
				mResult = "你胜利了";
			else if (myCount < heCount)
				mResult = "你失败了";
			else
				mResult = "你们旗鼓相当";
			if (MessageBox.Show(string.Format("游戏结束，{0}！！\r\n\r\n你得了 {1}分\r\n她得了 {2}分\r\n\r\n要重新开始吗？{1}", mResult, myCount, heCount), "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
			{
				this.Start();
			}
		}

		private bool heStart(PukeCard myCard)
		{
			isFirst = false;
			isHeOver = false;
			if (myCard == null)
			{
				if (he.Count == 0) { this.UpdateForm(); this.gameOver("he"); return false; }
				Random random = new Random();
				PukeCard temp = he[random.Next(he.Count)];
				heNumber = temp.Number;
				publicCard.Add(temp);
				he.Remove(temp);
			}
			else
			{
				PukeCard[] ps = new PukeCard[he.Count];
				he.CopyTo(ps);
				int tempCount = 0;
				foreach (PukeCard p in ps)
				{
					if (p.Number == myCard.NextNumber)
					{
						tempCount += 1;
						if (tempCount > NumberCount) break;
						heNumber = p.Number;
						publicCard.Add(p);
						he.Remove(p);
					}
				}
				if (tempCount < NumberCount)
				{
					isHeOver = true;
					heNumber = -1;
					btnSend.Enabled = false;
					btnGet.Enabled = true;
					btnGet.Text = "取牌";
					state = "get";
					return false;
				}
			}
			if (my.Count == 0)
			{
				this.state = "";
				this.btnGet_Click(null, null);
			}
			return true;
			//NumberCount = tempCount;
			//isFirst = true;
		}
		private void UpdateForm()
		{
			this.Controls.Clear();
			int mControlsCount = this.Controls.Count;
			
			this.Controls.Add(btnSend);
			this.Controls.Add(btnGet);
			my.Sort(PukeCardCompare.NumberDesc);
			for (int i = my.Count - 1; i >= 0; i--)
			{
				if (my[i].DoubleClickEventHandler == null)
					my[i].DoubleClickEventHandler = new EventHandler(this.btnSend_Click);
				my[i].Top = 190;
				my[i].Left = i * pukeInfo.OffsetX + 10;
				my[i].Clicked = true;
				my[i].Display = true;
				if (my[i].Selected)
					my[i].Top -= my[i].TopOffSet;
				this.Controls.Add(my[i]);
			}
			for (int i = myRecord.Count - 1; i >= 0; i--)
			{
				myRecord[i].Top = 190;
				myRecord[i].Left = i * 2 + 160;
				myRecord[i].Clicked = false;
				myRecord[i].Display = true;
				this.Controls.Add(myRecord[i]);
			}

			for (int i = publicCard.Count - 1; i >= 0; i--)
			{
				publicCard[i].Top = 100;
				publicCard[i].Left = i * pukeInfo.OffsetX + 10;
				publicCard[i].Clicked = false;
				publicCard[i].Display = true;
				this.Controls.Add(publicCard[i]);
			}

			he.Sort(PukeCardCompare.NumberAsc);
			for (int i = he.Count - 1; i >= 0; i--)
			{
				he[i].Top = 5;
				he[i].Left = i * pukeInfo.OffsetX + 10;
				he[i].Clicked = false;
				he[i].Display = false;
				this.Controls.Add(he[i]);
			}
			for (int i = heRecord.Count - 1; i >= 0; i--)
			{
				heRecord[i].Top = 5;
				heRecord[i].Left = i * 2 + 160;
				heRecord[i].Clicked = false;
				heRecord[i].Display = true;
				this.Controls.Add(heRecord[i]);
			}
		}
		#endregion
	}
}